/* GSManufactureWriteAction.java
 *
 * Project/Product: APEP-JZCG
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-10-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.sql.Connection;
import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.form.GSProductBidForm;
import com.hitrust.gs.form.GSProductTypeForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.DBConnectionManager;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSProductBidView;
import com.hitrust.gs.vo.GSProductTypeView;


import framework.core.ConstDefine;
import framework.core.DateUtil;

public class GSProductBidWriteAction extends GSBaseAction {

    /*
     * (non-Javadoc)
     * 
     * @see com.apep.jzcg.service.GSBaseAction#execute(com.apep.jzcg.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {

        GSProductBidForm form = (GSProductBidForm) aForm;
        LoginContext loginInfo = (LoginContext) form.getSession().getAttribute(GSBaseConstants.LOGIN);
        String[] index = form.getSel_manufacture();
        String[] rid = form.getGs_recorder_id();
        ResultProcess rst = (ResultProcess) aForm.getSession().getAttribute("manufactureExcel");
        ArrayList array = rst.getAllRecoreds();
        GSProductBidDao dao = new GSProductBidDao();
        GSManufactureProcessor proc = new GSManufactureProcessor();
        Connection con = null;
        try {
            con = DBConnectionManager.getInstance().getConnection(); 
            for (int a= 0; a < index.length; a++) {
    			int i = 0;
    			for (int j = 0; j < rid.length; j++) {
    				if(index[a].equals(rid[j])){
    					i = j;
    					break;
    				}
    			}
    			GSProductBidBean bean = dao.findProductBidByProuductCode(form.getProductCode()[i]);
          	  	GSManufactureBean mbean=proc.getManufInfoByConcentricityId(form.getProductCode()[i]);
                if (mbean==null) {
                    throw new GSException("已存在相同集中编码的产品信息,集中编码："+form.getProductCode()[i]);
                }
                boolean isFlag = false;
    			if(bean ==null){
              		bean = new GSProductBidBean();
                  	bean.setCreateTime(new DateUtil().getFullString());
                  	bean.setCreateUser(loginInfo.getLoginUserName());
                  	bean.setCreateUserId(loginInfo.getLoginUserId());
                  	bean.setProductName(form.getProductName()[i].trim());
                  	bean.setLastUpdateTime(new DateUtil().getFullString());
                  	bean.setLastUpdateUser(loginInfo.getLoginUserName());
                  	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
                  	bean.setProductCode(form.getProductCode()[i].trim());
                  	bean.setProductType(Integer.parseInt(form.getProductType()[i].trim()));
                  	bean.setProviderCode(mbean.getGs_provider_id().trim());
                  	bean.setProductId(mbean.getId());
                  	bean.setRemark(form.getRemark()[i]);
                  	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
                  	isFlag = dao.insertByConnection(bean,con);
              }else{
            	  	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
            	  	bean.setProductId(mbean.getId());
            	  	bean.setProductName(form.getProductName()[i].trim());
    	          	bean.setProductCode(form.getProductCode()[i].trim());
    	          	bean.setProductType(Integer.parseInt(form.getProductType()[i].trim()));
    	          	bean.setProviderCode(mbean.getGs_provider_id().trim());
    	          	bean.setLastUpdateTime(new DateUtil().getFullString());
    	          	bean.setLastUpdateUser(loginInfo.getLoginUserName());
    	          	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
    	        	bean.setRemark(form.getRemark()[i]);
    	          	isFlag = dao.updateByConnection(bean,con);
              }
    			GSProductTypeDao productTypeDao = new GSProductTypeDao();
    			GSProductTypeBean productTypeBean = productTypeDao.findProductTypeByProuductCode(form.getProductCode()[i]);
    			if(productTypeBean ==null){
    				productTypeBean = new GSProductTypeBean();
    				productTypeBean.setCreateTime(new DateUtil().getFullString());
    				productTypeBean.setCreateUser(loginInfo.getLoginUserName());
    				productTypeBean.setCreateUserId(loginInfo.getLoginUserId());
    				productTypeBean.setIsBalance(1);
    				productTypeBean.setIsBid(Integer.parseInt(form.getProductType()[i].trim()));
    				productTypeBean.setLastUpdateTime(new DateUtil().getFullString());
    				productTypeBean.setLastUpdateUser(loginInfo.getLoginUserName());
    				productTypeBean.setLastUpdateUserId(loginInfo.getLoginUserId());
    				productTypeBean.setProductCode(form.getProductCode()[i]);
    				productTypeBean.setProductType(GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_USE);
    				productTypeBean.setProviderCode(mbean.getGs_provider_id());
    				productTypeBean.setProductId(mbean.getId());
    				productTypeBean.setRemark(form.getRemark()[i]);
    				productTypeBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
                  	isFlag = productTypeDao.insertByConnection(productTypeBean,con);
              }else{
            	  productTypeBean.setIsBid(Integer.parseInt(form.getProductType()[i].trim()));
            	  productTypeBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
            	  productTypeBean.setProductId(mbean.getId());
            	  productTypeBean.setProductCode(form.getProductCode()[i]);
            	  productTypeBean.setProviderCode(mbean.getGs_provider_id());
            	  productTypeBean.setLastUpdateTime(new DateUtil().getFullString());
            	  productTypeBean.setLastUpdateUser(loginInfo.getLoginUserName());
            	  productTypeBean.setLastUpdateUserId(loginInfo.getLoginUserId());
    	          	isFlag = productTypeDao.updateByConnection(productTypeBean,con);
              }
    			if(isFlag){
    				// 移除
                    int row = array.size();
                    for (int j = 0; j < row; j++) {
                        GSProductBidView mv = (GSProductBidView) array.get(j);
                        if (index[a].equals(mv.getGs_recorder_id())) {
                            array.remove(mv);
                            break;
                        }
                    }
    			}
    	        if (array != null && array.size() == 0) {
    	            aForm.getSession().removeAttribute("manufactureExcel");
    	            aForm.getRequest().setAttribute("message", "产品确认写入成功，无可确认产品信息，返回继续添加。");
    	            aForm.getRequest().setAttribute("action", "A400340.do");
    	        } else if (array != null && array.size() > 0) {
    	            aForm.getRequest().setAttribute("message", "产品确认写入成功");
    	            if (aForm.getRequest().getParameter("choice").equals("")) {
    	                aForm.getRequest().setAttribute("action", "A400342.do");
    	            }
    	        }
    		}
    		con.commit();
        } catch (GSException e) {
        	con.rollback();
            GSLogWriter.error(this, e.toString());
            aForm.getRequest().setAttribute("action", "A400342.do");
            aForm.getRequest().setAttribute("target", "_self");   
            throw new GSException(e.getMessage());
        } finally {
        	if(con != null) {
    			con.close();
    		}
            rst.setResult(array);
            aForm.getSession().setAttribute("manufactureExcel", rst);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
